package com.swust.course.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.swust.common.security.utils.SecurityUtils;
import com.swust.course.domain.CourseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.swust.common.log.annotation.Log;
import com.swust.common.log.enums.BusinessType;
import com.swust.common.security.annotation.RequiresPermissions;
import com.swust.course.domain.CoursePlan;
import com.swust.course.service.ICoursePlanService;
import com.swust.common.core.web.controller.BaseController;
import com.swust.common.core.web.domain.AjaxResult;
import com.swust.common.core.utils.poi.ExcelUtil;
import com.swust.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 开课计划Controller
 * 
 * @author LiuShuai
 * @date 2024-04-05
 */
@RestController
@RequestMapping("/coursePlan")
public class CoursePlanController extends BaseController
{
    @Autowired
    private ICoursePlanService coursePlanService;

    /**
     * 查询开课计划列表
     */
    @RequiresPermissions("course:coursePlan:list")
    @GetMapping("/list")
    public TableDataInfo list(CoursePlan coursePlan)
    {
        startPage();
        List<CoursePlan> list = coursePlanService.selectCoursePlanList(coursePlan);
        return getDataTable(list);
    }

    @Log(title = "开课计划导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("course:coursePlan:export")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        final ExcelUtil<CoursePlan> coursePlanExcelUtil = new ExcelUtil<>(CoursePlan.class);
        final List<CoursePlan> courseInfos = coursePlanExcelUtil.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = coursePlanService.importCourseInfo(courseInfos, updateSupport, operName);
        return success(message);
    }

    /**
     * 开课计划导入模板
     *
     * @param response
     * @throws IOException
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        final ExcelUtil<CoursePlan> coursePlanExcelUtil = new ExcelUtil<>(CoursePlan.class);
        coursePlanExcelUtil.importTemplateExcel(response, "课程数据");
    }

    /**
     * 导出开课计划列表
     */
    @RequiresPermissions("course:coursePlan:export")
    @Log(title = "开课计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CoursePlan coursePlan)
    {
        List<CoursePlan> list = coursePlanService.selectCoursePlanList(coursePlan);
        ExcelUtil<CoursePlan> util = new ExcelUtil<CoursePlan>(CoursePlan.class);
        util.exportExcel(response, list, "开课计划数据");
    }

    /**
     * 获取开课计划详细信息
     */
    @RequiresPermissions("course:coursePlan:query")
    @GetMapping(value = "/{courseSerialNo}")
    public AjaxResult getInfo(@PathVariable("courseSerialNo") String courseSerialNo)
    {
        return success(coursePlanService.selectCoursePlanByCourseSerialNo(courseSerialNo));
    }

    /**
     * 新增开课计划
     */
    @RequiresPermissions("course:coursePlan:add")
    @Log(title = "开课计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CoursePlan coursePlan)
    {
        return toAjax(coursePlanService.insertCoursePlan(coursePlan));
    }

    /**
     * 修改开课计划
     */
    @RequiresPermissions("course:coursePlan:edit")
    @Log(title = "开课计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CoursePlan coursePlan)
    {
        return toAjax(coursePlanService.updateCoursePlan(coursePlan));
    }

    /**
     * 删除开课计划
     */
    @RequiresPermissions("course:coursePlan:remove")
    @Log(title = "开课计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseSerialNos}")
    public AjaxResult remove(@PathVariable String[] courseSerialNos)
    {
        return toAjax(coursePlanService.deleteCoursePlanByCourseSerialNos(courseSerialNos));
    }
}
